package org.dromara.chicken.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.chicken.domain.GrowInfo;
import org.dromara.chicken.mapper.GrowInfoMapper;
import org.dromara.chicken.service.IGrowInfoService;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 成长记录Service业务层处理
 *
 * @author Mr Lei QQ:235921049
 * @date 2024-06-14
 */
@RequiredArgsConstructor
@Service
public class GrowInfoServiceImpl implements IGrowInfoService {

    private final GrowInfoMapper baseMapper;

    /**
     * 查询成长记录
     */
    @Override
    public GrowInfo queryById(String id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询成长记录列表
     */
    @Override
    public TableDataInfo<GrowInfo> queryPageList(GrowInfo bo, PageQuery pageQuery) {
        Page<GrowInfo> result = baseMapper.queryPageList(pageQuery.build(), bo);
        return TableDataInfo.build(result);
    }

    /**
     * 查询成长记录列表
     */
    @Override
    public List<GrowInfo> queryList(GrowInfo bo) {
        LambdaQueryWrapper<GrowInfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<GrowInfo> buildQueryWrapper(GrowInfo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<GrowInfo> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getOrderId() != null, GrowInfo::getOrderId, bo.getOrderId());
        lqw.eq(bo.getProductsCateId() != null, GrowInfo::getProductsCateId, bo.getProductsCateId());
        lqw.eq(bo.getProductId() != null, GrowInfo::getProductId, bo.getProductId());
        lqw.eq(bo.getDrugNum() != null, GrowInfo::getDrugNum, bo.getDrugNum());
        lqw.eq(StringUtils.isNotBlank(bo.getStock()), GrowInfo::getStock, bo.getStock());
        lqw.eq(bo.getPrice() != null, GrowInfo::getPrice, bo.getPrice());
        lqw.eq(bo.getMoney() != null, GrowInfo::getMoney, bo.getMoney());
        return lqw;
    }

    /**
     * 新增成长记录
     */
    @Override
    public Boolean insertByBo(GrowInfo bo) {
        GrowInfo add = MapstructUtils.convert(bo, GrowInfo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改成长记录
     */
    @Override
    public Boolean updateByBo(GrowInfo bo) {
        GrowInfo update = MapstructUtils.convert(bo, GrowInfo.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(GrowInfo entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除成长记录
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
